﻿namespace NetFx.SMIL.References
{
  using System;
  using System.Text;
  using System.Collections;
  using System.Collections.Generic;

  using System.Reflection;
  using System.Reflection.Emit;
  using System.Runtime.InteropServices;

  using System.Diagnostics;
  using System.Diagnostics.CodeAnalysis;
  using System.Diagnostics.SymbolStore;
  using System.Security;
  using System.Security.Permissions;

  using NetFx.MSIL;

  /// <summary>
  /// 对局部变量的引用。
  /// </summary>
  public class VariableRef:ReflectRef
  {
    #region Statics
    public static string DefaultVariablelName = ".local";


    public new static VariableRef it()
    {
      return new VariableRef();
    }
    public static VariableRef of(string identifier)
    {
      return new VariableRef(identifier);
    }
    public static VariableRef of(string identifier, LocalVariableInfo variable)
    {
      return new VariableRef(identifier, variable);
    }
    #endregion
    #region Fields
    protected bool isArgument = false;
    #endregion
    #region Properties
    /// <summary>
    /// 获取引用类型（为局部变量）。
    /// </summary>
    public override DefType ReflectionType
    {
      get { return DefType.Variable; }
    }
    /// <summary>
    /// 获取反射信息。
    /// </summary>
    /// <remarks>
    /// 注意，在.NET 2.0 中，LocalBuilder 是LocalVariableInfo的派生类。
    /// </remarks>
    public virtual LocalVariableInfo ReflectInfo
    {
      get
      {
        return this.referenced as LocalVariableInfo;
      }
    }
    public override bool HasReflectInfo
    {
      get
      {
        return this.ReflectInfo != null;
      }
    }

    #endregion
    #region Constructors
    public VariableRef() : base(null) { }
    public VariableRef(string identifier) : base(identifier) { }
    public VariableRef(string identifier, bool isArgument) : base(identifier) 
    {
      this.isArgument = isArgument;
    }
    public VariableRef(string identifier, LocalVariableInfo variable) : base(identifier, variable) { }
    #endregion
    #region Methods
    protected override ReflectRef OnBinding(object source)
    {
      return base.OnBinding(source as LocalVariableInfo);
    }

    public override string ToString()
    {
      int index = this.HasReflectInfo ? this.ReflectInfo.LocalIndex : 0;

      return this.Identifier != null ?
        string.Format("{0}", this.Identifier) :
        string.Format("{0}_{1}",DefaultVariablelName, index);
    }
    #endregion
  }
}
